# -*- coding: utf-8 -*-
import json
import os
from multiprocessing import Pool
import pymongo
import numpy as np

host = 'localhost'
data_base = 'Lianjia'
client = pymongo.MongoClient(host)
t_xiaoqu = 'xiaoqu'
db = client[data_base]
base_dir = './long_lat/'


def query(id):
    try:
        file_path = base_dir + id + '.txt'
        if not os.path.exists(file_path):
            return
        with open(file_path) as f:
            obj = json.load(f)
            point_x = []
            point_y = []
            if 'detail' not in obj:
                print("detail " + id)
                return None
            if 'pois' not in obj['detail']:
                print('pois ' + id)
                return None
            for pois in obj['detail']['pois']:
                x = pois['pointx']
                y = pois['pointy']
                point_x.append(x)
                point_y.append(y)
            return {'longitude': np.mean(point_x), 'latitude': np.mean(point_y), 'id': id}

    except Exception as err:
        print(err)


def add_or_update_to_db(info):
    if info is None:
        return
    if db[t_xiaoqu].update_one({'小区编号': info['id']},
                               {'$set': {"longitude": info['longitude'], 'latitude': info['latitude']}}, True):
        print(info)


def main(info):
    long_lat = query(info['小区编号'])
    add_or_update_to_db(long_lat)


if __name__ == '__main__':
    print(db[t_xiaoqu].find({'latitude': None}).count())
    with Pool(os.cpu_count()) as p:
        p.map(main, [info for info in db[t_xiaoqu].find().batch_size(50)
                     if 'longitude' not in info or info['longitude'] is None])
